<?php

/**
 * 成功提示
 *
 * @param array $data
 * @param string $dataName
 * @param string $message
 * @param int $code
 *
 * @return \Illuminate\Http\JsonResponse
 */
function success($data = [], $dataName = 'data', $message = 'success', $code = 1)
{
    return response()->json(['message' => $message, 'code' => $code, "{$dataName}" => $data], 200);
}


/**
 * 错误提示
 *
 * @param string $msg
 * @param int $code
 * @param int $httpStatus
 *
 * @return \Illuminate\Http\JsonResponse
 */
function error($msg = '未知错误', $code = 0, $httpStatus = 422)
{
    return response()->json(['message' => $msg, 'code' => $code, 'data' => ''], $httpStatus);
}

/**
 * 生成6位随机数
 *
 * @return int
 */
function rand6()
{
    return rand(100000, 999999);
}


/**
 * 替换文本中的url 为 a标签
 *
 * @param string $str
 * @return null|string|string[]
 */
function replace_url_link(string $str)
{
    $re = '@((https|http)?://([-\w\.]+)+(:\d+)?(/([\w/_\-.#%]*(\?\S+)?)?)?)@';
    return preg_replace_callback($re, function ($matches) {
        return sprintf('<a href="%s" target="_blank">%s</a>', trim($matches[0], '&quot;'), $matches[0]);
    }, $str);
}